<template>
  <a-card :bordered="false">
      <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
           <template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="流程名称">
              <j-popup
                :multi="false"
                v-model="queryParam.processName"
                field="processName"
                org-fields="process_name,id"
                dest-fields="processName,processId"
                code="sys_process_report"
                />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="应用部门">
              <j-select-depart v-model="queryParam.applyDepart" multi/>
            </a-form-item>
          </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" :disabled="isDisabledAuth('erpProcessConfig:add')">新增</a-button>
      <a-button @click="startCopy" v-if="selectedRowKeys.length == 1" type="primary" icon="copy" :disabled="isDisabledAuth('erpProcessConfig:add')">复制</a-button>
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('流程配置表')" :disabled="isDisabledAuth('erpProcessConfig:export')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import" :disabled="isDisabledAuth('erpProcessConfig:import')">导入</a-button>
      </a-upload> -->
      <!-- 高级查询区域 -->
      <!--<j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="startCopy" :disabled="isDisabledAuth('erpProcessConfig:delete')"><a-icon type="copy"/>批量复制</a-menu-item>
          <a-menu-item key="1" @click="batchDel" :disabled="isDisabledAuth('erpProcessConfig:delete')"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
      <a-popover title="自定义列" trigger="click" placement="leftBottom">
        <template slot="content">
          <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
            <a-row>
              <template v-for="(item,index) in defColumns">
                <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                  <a-col :span="12"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                </template>
              </template>
            </a-row>
          </a-checkbox-group>
        </template>
        <a style="float: right;"><a-icon type="setting" :style='iconStyle'/></a>
      </a-popover>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        :customRow="customRow"
        @change="handleTableChange">
        <template slot="status" slot-scope="status">
           <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
            <a-button
                class="ruleButton"
                :ghost="true"
                type="primary"
                icon="edit"
                @click="handleEdit(record)"
                :disabled="isDisabledAuth('erpProcessConfig:edit')"
        />
        <a-divider type="vertical"/>
        <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a-button
                    class="ruleButton"
                    :ghost="true"
                    type="primary"
                    icon="delete"
                    :disabled="isDisabledAuth('erpProcessConfig:delete')"/>
        </a-popconfirm>
        </span>

      </a-table>
    </div>
    <!--批量复制选择应用部门-->
    <a-modal :visible="copySelectVisible" title="应用到部门" @ok="copyList" @cancel="cancelCopy">
      <j-select-depart placeholder="请选择应用部门" v-model="copyDept" :multi="false"  />
    </a-modal>
    <erp-process-config-modal ref="modalForm" @ok="modalFormOk"></erp-process-config-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ErpProcessConfigModal from './modules/ErpProcessConfigModal'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import {DisabledAuthFilterMixin} from '@/mixins/DisabledAuthFilterMixin'
  import {postAction} from '@/api/manage'
import { getAction } from '@/api/manage'
  export default {
    name: 'erpProcessConfigList',
    mixins:[JeecgListMixin, mixinDevice, DisabledAuthFilterMixin],
    components: {
      ErpProcessConfigModal,
    },
    data () {
      return {
        // 复制选择应用到部门
        copyDept: null,
        // 复制选择应用到部门弹窗
        copySelectVisible: false,
        processName:[],
        description: '流程配置表管理页面',
        //表头
        columns:[],
        //列设置
        settingColumns:[],
        // 列定义
        defColumns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'流程名称',
            align:"center",
            dataIndex: 'processName',
          },
          {
            title:'应用部门',
            align:"center",
            dataIndex: 'applyDepart_dictText',
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: {
            filterDropdown: 'filterDropdown',
            filterIcon: 'filterIcon',
            customRender: 'action' }
          }
        ],
        url: {
          add: "/api/erp/process/erpProcessConfig/saveOrUpdateProcess",
          list: "/api/erp/process/erpProcessConfig/list",
          delete: "/api/erp/process/erpProcessConfig/delete",
          deleteBatch: "/api/erp/process/erpProcessConfig/deleteBatch",
          exportXlsUrl: "/api/erp/process/erpProcessConfig/exportXls",
          importExcelUrl: "/api/erp/process/erpProcessConfig/importExcel",
          queryProcessById: '/api/erp/process/erpProcessConfig/queryProcessById'
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      // 选择部门后复制
      startCopy(){
        // 选择部门
        this.copySelectVisible = true;
      },
      cancelCopy(){
        this.copyDept = null;
        this.copySelectVisible = false;
      },
      // 批量复制
      copyList(){
        if(this.copyDept==null){
          return
        }
        this.copySelectVisible = false;
        // 异常处理
        if(this.selectedRowKeys.length===0){
          this.$message.warning("请稍后重试!");
        }
        this.selectedRowKeys.forEach(id => {
          // 根据选中的ID从列表中筛选
        let copyer = Object.assign({}, this.dataSource.filter(item=>item.id==id)[0]);
        // 查询节点列表
        getAction(this.url.queryProcessById,{id:copyer.id}).then(res=>{
          let erpProcessConfigMainVoList = [];
          let that = this;
          if(res.result.erpProcessConfigMainVoList&&res.result.erpProcessConfigMainVoList!=null&&res.result.erpProcessConfigMainVoList.length>0){
            res.result.erpProcessConfigMainVoList.forEach(vo=>{
              vo.execDepart = that.copyDept;
              erpProcessConfigMainVoList.push(vo)
            })
          }
          this.$set(copyer,'erpProcessConfigMainVoList',erpProcessConfigMainVoList);
          this.$set(copyer,'id',null);
          // 去除应用部门
          this.$set(copyer,'applyDepart',this.copyDept);
          this.$set(copyer,'applyDepart_dictText',null);
          // 添加进列表
          postAction(this.url.add,copyer).then(res=>{
            // 添加成功刷新列表
            if(res.result){
              this.loadData(1);
              this.selectedRowKeys = [];
            }
          }).finally(()=>this.copyDept=null)
        })
        });
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'processId',text:'流程定义id',dictCode:''})
        fieldList.push({type:'popup',value:'processName',text:'流程名称', popup:{code:'sys_process_report',field:'processName',orgFields:'processName',destFields:'process_name'}})
        fieldList.push({type:'string',value:'applyDepart',text:'应用部门',dictCode:'sys_depart,depart_name,id'})
        fieldList.push({type:'string',value:'isUpdate',text:'流程是否更新',dictCode:'YES_OR_NO'})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
